<template>
  <div class="login_container">
    <div class="login_box">
      <h3>在役检查管理系统</h3>
      <el-form ref="loginFormRef" :model="loginForm" label-width="0px" class="login_form" :rules="loginFormRules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="请输入用户名" clearable></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'P186382',
        password: 'P186382'
      },
      loginFormRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  created() {},
  methods: {
    // 重置loginForm
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请输入用户名和密码')
        }
        const { data: res } = await this.$http.post('user/login', this.loginForm)
        // const { data: res } = await this.$http.post('user/login', this.loginForm, {headers: {'Content-Type': 'multipart/form-data'}})
        // const { data: res } = await this.$http.post('user/login', this.loginForm, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
        // console.log(res)
        if(res.meta.status !==200){
          return this.$message.error(res.meta.message)
        }
        const token = res.data.token
        window.sessionStorage.setItem('token', token)
        this.$message.success(res.meta.message)
        this.$router.push("/home")
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background: #2b4b6b;
  height: 100%;
  position: relative;
}

.login_box {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
h3 {
  text-align: center;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
